import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;

import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.Timer;

public class Game extends JPanel implements ActionListener {
	private Menu mainMenu;
	private int game;
	private Timer timer;
	private InGame newGame;
	private JOptionPane hello;
	
	public boolean power;

	public final int IN_MAIN_MENU = 1;
	public final int IN_GAME = 2;
	public final int IN_LOAD = 3;
	public final int IN_OPTIONS = 4;

	public Game() {
		power = false;
		addMouseListener(new MouseInput());
		addKeyListener(new TAdapter());
		setFocusable(true);
		setBackground(Color.gray);
		System.out.println("" + getKeyListeners());

		game = 1;

		mainMenu = new Menu(200, 20, this);
		mainMenu.setBackground("images/menubg.png");

		mainMenu.createItem(95, 80, "images/newgame.png");
		mainMenu.createNext("images/loadgame.png");
		mainMenu.createNext("images/options.png");
		mainMenu.createNext("images/exit.png");

		timer = new Timer(20, this);
		timer.start();
	}

	public void paint(Graphics g) {
		super.paint(g);
		
		Graphics2D g2d = (Graphics2D) g;

		if (game == IN_MAIN_MENU)
			mainMenu.draw(g2d);
		else if (game == IN_GAME) {
			newGame.draw(g2d, this);
		} else if (game == IN_LOAD) {

		} else if (game == IN_OPTIONS) {
		}
	}

	public int getGame() {
		return game;
	}

	public void setGame(int game) {
		this.game = game;
	}

	public void newGame() {
		newGame = new InGame(this);
		setGame(IN_GAME);
	}

	public void exitGame() {
		System.exit(0);
	}

	public void actionPerformed(ActionEvent e) {
		repaint();
		if (game == IN_GAME)
			newGame.tick();
	}

	private class TAdapter extends KeyAdapter {
		public void keyReleased(KeyEvent key) {
			if (game == IN_GAME)
				newGame.keyReleased(key);
		}

		public void keyPressed(KeyEvent key) {
			if (game == IN_GAME)
				newGame.keyPressed(key);
		}
	}

	private class MouseInput extends MouseAdapter {

		public void mousePressed(MouseEvent e) {
			int x = e.getX();
			int y = e.getY();			
			if (game == IN_MAIN_MENU) {
				switch (mainMenu.mouseClick(x, y)) {
				case 0:
					newGame();
					break;
				case 1:
					// loadGame();
					break;
				case 2:
					// options();
					break;
				case 3:
					exitGame();
				}
			}
		}
	}
}
